@extends('shop_admin.admin')

@section('content')
    <script src="{{ asset ("/components/blueimp-file-upload/js/vendor/jquery.ui.widget.js") }}"></script>
    <script src="{{ asset ("/components/blueimp-file-upload/js/jquery.iframe-transport.js") }}"></script>
    <script src="{{ asset ("/components/blueimp-file-upload/js/jquery.fileupload.js") }}"></script>


        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        图片上传demo
        <small>添加</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/shop_admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li class="active">图片上传demo</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">图片上传demo</h3>

                </div>
                <!-- /.box-header -->
                <!-- form start -->
                    <div class="box-body">
                        <div class="form-group @if($errors->has('attribute_name')) has-error   @endif">
                            <div style="width: 170px;">
                                <div class="box  box-default image-uploader">
                                    <style>
                                        .upload-btn{
                                            display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                        }
                                    </style>
                                    <!-- /.box-header -->
                                    <div class="box-body">

                                        <div style="width: 145px;height: 145px; display: block;">
                                            <img class="image" style="width: 145px;; padding: 1px;" width="145" height="145" src="http://placehold.it/145x145"/>
                                        </div>
                                        <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                        </div>

                                        <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                            <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.box-body -->
                                    <div class="box-footer" style="position: relative">

                                        <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                        {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/shop_admin/file_upload']) !!}
                                        {!! Form::text('thumb',old('thumb'),['style'=>'display:none;','id'=>'thumb']) !!}

                                        <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                    </div>
                                </div>
                                <!-- /.box -->
                            </div>

                            @if($errors->has('attribute_name'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('attribute_name')}}</label>
                            @endif
                        </div>

                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary">保存</button>
                    </div>
            </div>
            <!-- /.box -->
        </div>
    </div>



</section>
<!-- /.content -->
    <script>
        $(function () {
            $('.image-uploader').each(function(k,imageUploader){
                var wrapper = $(imageUploader);//盒子
                var uploadBtn = wrapper.find('.upload-btn');
                var imageFile = wrapper.find('.image');
                var progress = wrapper.find('.progress');
                var progressBar = progress.find('.progress-bar');
                var inputFile = wrapper.find('#thumb');
                var message = wrapper.find('.message');

                uploadBtn.click(function(){
                    progressBar.css('width', 0 + '%');
                    imageFile.attr('src','http://placehold.it/145x145');
                    inputFile.val('');
                    message.hide();

                });
                wrapper.find('.btn-danger').click(function(){
                    progressBar.css('width', 0 + '%');
                    imageFile.attr('src','http://placehold.it/145x145');
                    inputFile.val('');
                    message.hide();

                });

                uploadBtn.fileupload({
                    dataType: 'json',
                    headers:{'X-CSRF-TOKEN':'{{csrf_token()}}'},
                    add: function (e, data) {
                        data.submit();
                    },
                    progressall: function (e, data) {
                        progress.fadeIn();
                        var percent = parseInt(data.loaded / data.total * 100, 10);
                        if (progress==1){
                            progress.show()
                        }
                        progressBar.css('width', percent + '%');
                        setTimeout(function () {
                            progress.fadeOut();
                        }, 500);
                    },
                    done: function (e, data) {
                        imageFile.attr('src',data.result.url);
                        inputFile.val(data.result.id);
                    },
                    complete:function(){
                    }
                });
            });
        });
    </script>
@endsection